// JScript 文件
var G = {
	getPager : function(totalCount, currentPage, pageSize, pagebarSize,
			pageFunction) {
		if (totalCount == 0)
			return "";

		//var div = document.createElement('div');

		var divLeft = document.createElement('div');
		divLeft.setAttribute("class", "left");

		var divRight = document.createElement('div');
		divRight.setAttribute("class", "right");

		var endPage = Math.floor(totalCount / pageSize);
		if ((totalCount % pageSize) > 0)
			endPage += 1;

		var prevPages = Math.floor((pagebarSize - 1) / 2 + 0.5);
		if (prevPages >= currentPage)
			prevPages = currentPage - 1;
		if (prevPages < 0)
			prevPages = 0;

		var pageStart = currentPage - prevPages;
		if ((pageStart + pagebarSize) > endPage)
			pageStart = endPage - pagebarSize + 1;
		if (pageStart < 1)
			pageStart = 1;

		var pagetEnd = currentPage - prevPages + pagebarSize - 1;
		if (pagetEnd > endPage)
			pagetEnd = endPage;

		// 上一頁是否可點
		var prev = null, prevAll = null;
		if (currentPage == 1) {
			prev = document.createElement('span');
			prevAll = document.createElement('span');
			//prevAll.attr("class","active");
		} else {
			prev = document.createElement('a');
			prev.href = "javascript:" + pageFunction + "(" + (currentPage - 1)
					+ ")";
			prevAll = document.createElement('a');
			prevAll.href = "javascript:" + pageFunction + "(" + 1 + ")";
		}
		prevAll.innerHTML = "第一页";
		prev.innerHTML = "上一页";
		
		var liPrevAll = $("<li>");
		liPrevAll.append(prevAll);
		var liPrev = $("<li>");
		liPrev.append(prev);
		$(divRight).append(liPrevAll);
		$(divRight).append(liPrev);

		// 頁次展開
		for (var i = pageStart; i <= pagetEnd; i++) {
			var pageItem = null;
			var lipageItem = $("<li>");
			if (i == currentPage) {
				$(lipageItem).addClass("active");
				pageItem = document.createElement('span');
			} else {
				pageItem = document.createElement('a');
				pageItem.href = "javascript:" + pageFunction + "(" + i + ")";
			}
			pageItem.innerHTML = i;
			lipageItem.append(pageItem);
			$(divRight).append(lipageItem);
		}

		// 下一頁是否可點
		var next = null, nextAll = null;
		if (currentPage == endPage) {
			next = document.createElement('span');
			nextAll = document.createElement('span');
		} else {
			next = document.createElement('a');
			next.href = "javascript:" + pageFunction + "(" + (parseInt(currentPage) + 1)+ ")";
			nextAll = document.createElement('a');
			nextAll.href = "javascript:" + pageFunction + "(" + endPage + ")";
		}
		next.innerHTML = "下一页";
		nextAll.innerHTML = "最后页";
		
		var liNextAll = $("<li>");
		liNextAll.append(nextAll);
		var liNext = $("<li>");
		liNext.append(next);
		$(divRight).append(liNext);
		$(divRight).append(liNextAll);

		// 分页细则 ex:共19条/每页 10 条 第 1/2 页
		divLeft.innerHTML = "共<span>" + totalCount + "</span>条/每页 <span>"
				+ pageSize + "</span> 条 第 <span>" + currentPage
				+ "</span>/<span>" + endPage + "</span> 页";
		//div.appendChild(divLeft);
		//div.appendChild(divRight);
		return divRight.innerHTML;
	}
};